<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class School_campus extends MY_Controller {

	public $left_menu_min;

	function __construct(){
	parent::__construct();

		$parent_id = 12;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "基础设置", "curr" => '/school_campus', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", 'setting');

	}

	/**
	 * 校区list
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	public function index($value='')
	{
		$this->load->model('Schoolo2o_model');
		$campus = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
		$this->smarty->assign("campus", $campus);

		
		$this->smarty->display("school_campus/list.html");
	}

	/**
	 * 添加校区
	 * @param string $value [description]
	 */
	public function add($value='')
	{	
		$this->smarty->assign("id", "0");
		$this->smarty->assign("post_url", "/school_campus/add");

		$post_data = $this->input->post();
		if($post_data){
			if(empty($post_data['name'])){
				$this->smarty->assign("name_err", "不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("name", $post_data['name']);
			}
			if(empty($post_data['schoolmaster'])){
				$this->smarty->assign("schoolmaster_err", "不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("schoolmaster", $post_data['schoolmaster']);
			}
			if(empty($post_data['tel'])){
				$this->smarty->assign("tel_err", "不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("tel", $post_data['tel']);
			}
			if(!empty($post_data['address'])){
				$this->smarty->assign("address", $post_data['address']);
			}

			//添加校区
			$this->load->model('Schoolo2o_model');
			$result = $this->Schoolo2o_model->add_school_campus($post_data,$this->user_data['school_id']);
			header("Location:/school_campus");exit;
		}

		
		$this->smarty->display("school_campus/add.html");
	}

	/**
	 * 添加校区
	 * @param string $value [description]
	 */
	public function edit($id)
	{	
		$this->smarty->assign("id", $id);
		$this->smarty->assign("post_url", "/school_campus/edit/{$id}");
		$this->load->model('Schoolo2o_model');
		
		//获取校区信息
		$campus = $this->Schoolo2o_model->get_school_campus_by_id($id);
		if(empty($campus)){
			header("Location:/school_campus");exit;
		}elseif ($campus['school_id']!=$this->user_data['school_id']) {
			header("Location:/school_campus");exit;
		}

		$post_data = $this->input->post();
		if($post_data){
			if(empty($post_data['name'])){
				$this->smarty->assign("name_err", "不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("name", $post_data['name']);
			}
			if(empty($post_data['schoolmaster'])){
				$this->smarty->assign("schoolmaster_err", "不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("schoolmaster", $post_data['schoolmaster']);
			}
			if(empty($post_data['tel'])){
				$this->smarty->assign("tel_err", "不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("tel", $post_data['tel']);
			}
			if(!empty($post_data['address'])){
				$this->smarty->assign("address", $post_data['address']);
			}

			//修改校区
			
			$result = $this->Schoolo2o_model->edit_school_campus($id,$post_data);
			header("Location:/school_campus");exit;
		}

		$this->smarty->assign("name", $campus['name']);
		$this->smarty->assign("schoolmaster", $campus['schoolmaster']);
		$this->smarty->assign("tel", $campus['tel']);
		$this->smarty->assign("address", $campus['address']);


		
		$this->smarty->display("school_campus/add.html");
	}

	/**
	 * 删除校区
	 * @param string $value [description]
	 */
	public function del($id)
	{	
		$this->smarty->assign("id", $id);
		
		$this->load->model('Schoolo2o_model');
		//获取校区信息
		$campus = $this->Schoolo2o_model->get_school_campus_by_id($id);
		if(empty($campus)){
			header("Location:/school_campus");exit;
		}elseif ($campus['school_id']!=$this->user_data['school_id']) {
			header("Location:/school_campus");exit;
		}

		//删除
		$this->Schoolo2o_model->delete_campus($id,$this->user_data['school_id']);
		header("Location:/school_campus");exit;
	}

}